package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.BigDataService;
import com.tanhua.app.service.QuanZiService;
import com.tanhua.domain.UserHolder;
import com.tanhua.domain.mogo.Publish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class QuanZiController {

    @Autowired
    private QuanZiService quanZiService;

    @PostMapping(value = "/movements", name = "发布动态")
    public ResponseEntity movements(Publish publish, MultipartFile[] imageContent) throws ClientException, IOException {
        return quanZiService.movements(publish,imageContent);
    }


    @GetMapping(value = "/movements/all", name = "查看我的动态")
    public ResponseEntity findAlbumByPage(Integer page,Integer pagesize,Long userId) throws ClientException, IOException {
        return quanZiService.findAlbumByPage(page,pagesize,userId);
    }




    @GetMapping(value = "/movements/recommend", name = "查看我的推荐动态")
    public ResponseEntity findRecommendByPage(Integer page,Integer pagesize) throws ClientException, IOException {
        return quanZiService.findRecommendByPage(page,pagesize, UserHolder.findUser().getId());
    }




    @GetMapping(value = "/movements", name = "查看我的好友动态")
    public ResponseEntity findTimeLineByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer pagesize) throws ClientException, IOException {

        return quanZiService.findTimeLineByPage(page,pagesize, UserHolder.findUser().getId());
    }



    @GetMapping(value = "/comments", name = "查看评论--评论回显")
    public ResponseEntity findComments(String movementId,Integer page,Integer pagesize) throws ClientException, IOException {
        System.out.println(movementId);
        return quanZiService.findComments(movementId,page,pagesize);
    }






    /*整理下当前登录用户的个人历史行为（收集给大数据）*/

    @Autowired
    private BigDataService bigDataService;

    //+1
    @GetMapping(value = "/movements/{id}", name = "查看评论--动态回显")
    public ResponseEntity findByPublishId(@PathVariable("id") String publishId) throws ClientException, IOException {

        bigDataService.toMq(1,publishId);

        return quanZiService.findByPublishId(publishId);
    }

    //+5
    @GetMapping(value = "/movements/{id}/like", name = "点赞")
    public ResponseEntity commentLike(@PathVariable("id") String publishId) throws ClientException, IOException {
        bigDataService.toMq(2,publishId);
        return quanZiService.commentLike(publishId);
    }

    //+8
    @GetMapping(value = "/movements/{id}/love", name = "喜欢")
    public ResponseEntity commentLove(@PathVariable("id") String publishId) throws ClientException, IOException {
        bigDataService.toMq(3,publishId);
        return quanZiService.commentLove(publishId);
    }

    //+10
    @PostMapping(value = "/comments", name = "发表评论")
    public ResponseEntity addComments(@RequestBody Map map) throws ClientException, IOException {
        String publishId =(String) map.get("movementId");
        String content =(String) map.get("comment");
        bigDataService.toMq(4,publishId);
        return quanZiService.addComments(publishId,content);
    }

    //-5
    @GetMapping(value = "/movements/{id}/dislike", name = "取消点赞")
    public ResponseEntity dislikeLike(@PathVariable("id") String publishId) throws ClientException, IOException {
        bigDataService.toMq(5,publishId);
        return quanZiService.dislikeLike(publishId);
    }

    //-8
    @GetMapping(value = "/movements/{id}/unlove", name = "取消喜欢")
    public ResponseEntity unLove(@PathVariable("id") String publishId) throws ClientException, IOException {
        bigDataService.toMq(6,publishId);
        return quanZiService.unLove(publishId);
    }
}
